package com.lqzs.businessmanager.ui.main.vm

import android.annotation.SuppressLint
import android.app.Activity
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.lqzs.businessmanager.BusinessManagerApplication
import com.lqzs.businessmanager.Config
import com.lqzs.businessmanager.base.BaseViewModel
import com.lqzs.businessmanager.bean.ReplyInfo
import com.lqzs.businessmanager.bean.response.LoginShopResponse
import com.lqzs.businessmanager.bean.response.PlatfromResponse
import com.lqzs.businessmanager.bean.response.VenDorResponse
import com.lqzs.businessmanager.dao.AccessDataBase
import com.lqzs.businessmanager.http.getLatestReView
import com.lqzs.businessmanager.http.getUnReplyReviews
import com.lqzs.businessmanager.http.loginShop
import com.lqzs.businessmanager.http.makeRequest
import com.lqzs.businessmanager.http.requestPlatformList
import com.lqzs.businessmanager.http.requestVenDorList
import com.lqzs.businessmanager.utils.PreferencesUtil
import com.lqzs.businessmanager.utils.StringUtils
import com.lqzs.businessmanager.utils.ThreadPoolHelper
import com.lqzs.businessmanager.utils.showCustomCenterToast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


class MainViewModel(mainActivity: Activity) : BaseViewModel() {
    companion object{
        val TAG: String = MainViewModel::class.java.name
    }
    @SuppressLint("StaticFieldLeak")
    val mActivity: Activity = mainActivity
    val shopId: MutableLiveData<List<LoginShopResponse>> = MutableLiveData()
    val venDorList: MutableLiveData<List<VenDorResponse>> = MutableLiveData()
    val platFormList: MutableLiveData<List<PlatfromResponse>> = MutableLiveData()

    fun getVenDorList() {
        viewModelScope.launch(Dispatchers.IO) {
            makeRequest(requestVenDorList())?.let {
                venDorList.postValue(it)
            }
        }
    }

    fun getPlatfromList(shopPhone: String) {
        viewModelScope.launch(Dispatchers.IO) {
            makeRequest(requestPlatformList())?.let {
                platFormList.postValue(it)
            }
        }
    }


    fun bindingShop(shopPhone: String) {
        viewModelScope.launch(Dispatchers.IO) {
            val loginShop = makeRequest(loginShop(mActivity, shopPhone))
            loginShop?.let {
                shopId.postValue(it)
            }
        }
    }

    suspend fun getLatestReView() {
        val latestReView = makeRequest(getLatestReView(mActivity))
        if (latestReView != null) {
            latestReView.review_date?.let {
                PreferencesUtil.putString(mActivity, Config.LAST_PINGLUN_TIME, it)
            }
            latestReView.review_content?.let {
                PreferencesUtil.putString(mActivity, Config.LAST_PINGLUN_CONTENT, it)
            }
        } else {
            withContext(Dispatchers.Main) {
                showCustomCenterToast(mActivity, "网络请求失败")
            }
        }
    }

    fun getUnReplyReview(branchId: Int, vendorId: Int, vendorName: String?) {
        viewModelScope.launch(Dispatchers.IO) {
            val unReplyReviews = makeRequest(getUnReplyReviews(branchId))
            unReplyReviews?.let {
                Log.e("aa", "getUnReplyReviews: " + unReplyReviews.size)
                if (unReplyReviews.isEmpty()) {
                    return@let
                }
                val unReplyReviewList = mutableListOf<ReplyInfo>()
                for (review in unReplyReviews) {
                    when (review.platform_name) {
                        "美团外卖"->{
                            if (!review.info.isNullOrEmpty()) {
                                for (info in review.info){
                                    val replyInfo = ReplyInfo()
                                    replyInfo.replyContent = info.reply_content
                                    replyInfo.reviewDate = StringUtils.getTime(info.review_date)
                                    replyInfo.reviewId = info.review_id
                                    replyInfo.reviewer = info.reviewer
                                    replyInfo.state = info.state
                                    replyInfo.reviewerInfos = info.review_content
                                    replyInfo.platformName= review.platform_name
                                    replyInfo.branchId = branchId
                                    replyInfo.branchName = vendorName
                                    replyInfo.id = replyInfo.key
                                    unReplyReviewList.add(replyInfo)
                                }
                            }
                        }
                        "饿了么"->{
                            if (!review.info.isNullOrEmpty()) {
                                for (info in review.info){
                                    val replyInfo = ReplyInfo()
                                    replyInfo.replyContent = info.reply_content
                                    replyInfo.reviewDate = StringUtils.getTime(info.review_date)
                                    replyInfo.reviewId = info.review_id
                                    replyInfo.reviewer = info.reviewer
                                    replyInfo.state = info.state
                                    replyInfo.reviewerInfos = info.review_content
                                    replyInfo.platformName= review.platform_name
                                    replyInfo.branchId = branchId
                                    replyInfo.branchName = vendorName
                                    replyInfo.id = replyInfo.key
                                    unReplyReviewList.add(replyInfo)
                                }
                            }
                        }
                        "美团到店"->{
                            if (!review.info.isNullOrEmpty()) {
                                for (info in review.info){
                                    val replyInfo = ReplyInfo()
                                    replyInfo.replyContent = info.reply_content
                                    replyInfo.reviewDate = StringUtils.getTime2(info.review_date)
                                    replyInfo.reviewId = info.review_id
                                    replyInfo.reviewer = info.reviewer
                                    replyInfo.state = info.state
                                    replyInfo.reviewerInfos = info.review_content
                                    replyInfo.platformName= review.platform_name
                                    replyInfo.branchId = branchId
                                    replyInfo.branchName = vendorName
                                    replyInfo.id = replyInfo.key
                                    unReplyReviewList.add(replyInfo)
                                }
                            }
                        }
                    }
                }
                if (unReplyReviewList.isNotEmpty()) {
                    val dao = AccessDataBase.getDataBase().replayDao()
                    for (replay in unReplyReviewList) {
                        val replyInfo = dao.query(replay.id)
                        replyInfo?.let {
                            dao.delete(it.id)
                        }
                        Log.e(TAG,"unReplyReview: $replay")
                        dao.insert(replay)
                    }
                }
            }
        }
    }

    fun getUnReplyReviewRecycle() {
        ThreadPoolHelper.execute({
            val vendorInfo = BusinessManagerApplication.getVendorInfo()
            vendorInfo?.let {
                getUnReplyReview(vendorInfo.branch_id, vendorInfo.vendor_id, vendorInfo.vendor_name)
            }
            getUnReplyReviewRecycle()
        }, (5 * 60 * 1000).toLong())
    }
}